package com.ellricky.tv.ui

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.andr.common.ui.mvp.BasePresenter
import com.andr.common.util.ActivityUtil
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.entity.course.FileSearchEntity
import com.ellricky.baselib.entity.new.CourseType
import com.ellricky.baselib.http.Api.API_TV_COURSE_TYPE
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.baselib.util.NewEListCallback
import com.ellricky.tv.Const
import com.ellricky.tv.R
import com.ellricky.tv.adapter.TvCourseTypeAdapter
import com.ellricky.tv.util.TvECallback
import com.tencent.bugly.crashreport.CrashReport
import kotlinx.android.synthetic.main.tv_activity_course_type_select.*
import kotlinx.android.synthetic.main.tv_activity_course_type_select.rvCourse
import java.util.HashMap

class TvCourseTypeSelectActivity : BaseTvActivity<Void, BasePresenter<Void>>() {

    private val mCourseAdapter by lazy {
        TvCourseTypeAdapter(this)
    }

    override fun getContentLayout(): Int {
        return R.layout.tv_activity_course_type_select
    }

    override fun initUIView() {
        ActivityUtil.addActivity(this)

        initRv()
//        CrashReport.testJavaCrash()
    }

    override fun fetchData() {
        fetchCourseType()
    }

    override fun initOnclick() {
        tvLogout.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        when (v?.id) {
            R.id.tvLogout -> logout()
        }
    }

    private fun fetchCourseType() {

        val url = EAppUtil.getETvApiHost()
        val paramMap: MutableMap<String, String> = HashMap()
        paramMap["action"] = "teacherCourseTypeList"
        paramMap["eid"] = LocalCacheUtil.getUid()

        HttpUtils.getInstance()
            .postFrom(url, paramMap, object : NewEListCallback<CourseType>(this) {
                override fun onSuccess(data: MutableList<CourseType>, msg: String?) {
                    if (data.isNotEmpty()) {
                        mCourseAdapter.refresh(data)
                    }
                }
            })
    }

    private fun initRv() {
        mCourseAdapter.also {
            rvCourse.adapter = it
            rvCourse.layoutManager = GridLayoutManager(this, 2)

            it.setOnItemClickListener { _,
                                        _,
                                        position,
                                        _ ->

                val data = it.dataSource[position]
                val bundle = Bundle()
                bundle.putString("course_type_id", data.course_id)
                bundle.putString("course_name", data.course_name)
                EAppUtil.jumpPageWithParam(this, TvAgeSelectActivity::class.java, bundle)
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        ActivityUtil.removeActivity(this)
        HttpUtils.getInstance().cancelTag(this)
    }
}